﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebUI.SmartDatingRef;
using System.IO;

namespace WebUI {
    public class Photo : BasePage {
        private SmartDatingClient smdc = new SmartDatingClient();

        protected void Page_Load(object sender, EventArgs e) {
            string size = "_small";
            if (Request["large"] != null) {
                size = "";
            }

            Guid usrGuid = new Guid(Request["id"]);
            string sessionKeyHash = (string)Session["sessionKeyHash"];

            ResultWithStatusOfstring getRes = Client.GetImage(sessionKeyHash, usrGuid);
            byte[] imageBuff;

            if (getRes.Result == null) {
                // could not get the image, write generic image
                Context.Response.ContentType = "image/png";

                // try to get at least user's gender
                ResultWithStatusOfint genderRes = Client.GetGender(sessionKeyHash, usrGuid);
                string gender = genderRes.Result.ToString();
                FileStream fs = new FileStream(Context.Server.MapPath(
                    "~/Images/no_photo" + size + "_" + gender + ".png"), FileMode.Open);
                imageBuff = new byte[fs.Length];
                fs.Read(imageBuff, 0, (int)fs.Length);
                fs.Close();
            } else {
                Context.Response.ContentType = "image/jpeg";
                imageBuff = b(getRes.Result);
            }

            Context.Response.OutputStream.Write(imageBuff, 0, imageBuff.Length);
            Context.Response.End();
        }
    }
}